<?php
declare(strict_types=1);

namespace App\Controller\Admin\V1\Paper;

use App\Controller\Admin\AdminController;
use App\Model\Question;
use App\Repositories\PaperQuestionRepository;
use App\Repositories\PaperRepository;
use App\Repositories\QuestionRepository;
use App\Repositories\UserPaperAnswerRepository;
use App\Repositories\UserPaperRepository;
use App\Repositories\UserRepository;
use App\Request\StorePaperRequest;
use App\Request\StoreQuestionRequest;
use Hyperf\DbConnection\Db;

class PaperController extends AdminController
{
    public function list(){
        $name = $this->request->input('title',null);
        $rows = $this->request->input('rows',10);
        $search=[];
        if($name){
            $search['name']=[
                'field' => 'title',
                'value' => $name,
                'method' => 'like'
            ];
        }
        $orderBy= [
            [
                'field' => 'id',
                'sort' => 'desc'
            ]
        ];
        $datas = PaperRepository::getInstance()->getList('*',$search,['limit'=>$rows],$orderBy);
        return $this->response->success($datas);
    }

    public function detail(){
        $id = $this->request->input('id');
        $paper = PaperRepository::getInstance()->getById($id);
        $questions = PaperQuestionRepository::getInstance()->all('*',[[
            'field' => 'paper_id',
            'value' => $id
        ]],[[
            'field' => 'listorder',
            'sort' => 'asc'
        ]]);
        $paper['questions'] = $questions;
        return $this->response->success($paper);
    }

    public function delete(){
        $id = $this->request->input('id');
        $result = PaperRepository::getInstance()->delete($id);
        if($result){
            return $this->response->success($result['data']);
        }
        return $this->response->error(10000,$result['msg']);
    }

    public function deleteQuestion(){
        $id = $this->request->input('id');
        $result = PaperQuestionRepository::getInstance()->delete($id);
        if($result){
            return $this->response->success($result['data']);
        }
        return $this->response->error(10000,$result['msg']);
    }

    public function store(StorePaperRequest $request){
        $admin = $this->request->getAttribute('admin');
        $params = $request->all();
        $params['admin_id'] = $admin->id;
        DB::beginTransaction();
        try{
            $result = PaperRepository::getInstance()->store($params);
            if(!$result['success']){
                throw new \Exception('问卷保存失败');
            }
            $paper = $result['data'];
            DB::commit();
        }catch(\Throwable $e){
            DB::rollBack();
            return $this->response->error(10012,$e->getMessage());
        }
        return $this->response->success([
            'id' => $paper->id
        ]);
    }

    public function statics(){
        $id = $this->request->input('id',0);
        $paper = PaperRepository::getInstance()->getById($id);
        if(!$paper){
            return $this->response->error(100404,'数据不存在');
        }
        $questions = PaperQuestionRepository::getInstance()->all('*',[[
            'field'=>'paper_id',
            'value' => $id
        ]],[[
            'field' => 'listorder',
            'sort' => 'asc'
        ]]);
        $all_count = UserPaperRepository::getInstance()->count('id',[[
            'field' => 'paper_id',
            'value' => $id
        ]]);
        foreach ($questions as &$item){
            if($item->input_type!=1){
                $item->options = explode(',',$item->options);
                $option_statics=[];
                foreach($item->options as $key=>$option){
                    $statcis=[];
                    $statcis['question_id'] = $item->id;
                    $statcis['index'] = $key;
                    $statcis['option'] = $option;
                    $count = UserPaperAnswerRepository::getInstance()->count('id',[[
                        'field' => 'question_id',
                        'value' => $item->id,
                    ],[
                        'field'=>'answer_txt',
                        'value' => $option
                    ]]);
                    $statcis['count'] = $count;
                    $statcis['ratio'] = $all_count?sprintf('%.2f',$count/$all_count*100).'%':'0%';
                    $option_statics[] = $statcis;
                }
                $item->option_statics = $option_statics;
            }
        }
        return $this->response->success([
            'paper' => $paper,
            'questions'=> $questions,
            'all_count' => $all_count
        ]);
    }

}